<?php
namespace Home\Model;
use Think\Model;


class FilmDownloadLinkModel extends Model{
	private $Table;
	public function __construct(){
		
	}
	
	
	public function InsertFilmLink($film_id,$film_link,$pic_url){
		if(is_array($film_link)){
			$datalist = array();
			foreach($film_link as $key => $val){
				$datalist[] = array("film_id"=>$film_id,"film_link"=>$val,"pic_url"=>$pic_url,"create_time"=>time());
			}
			$Table = M("FilmDownloadLink");		
			$Table->addAll($datalist);		
		}

	}
	public function insert_film_link($film_id,$link){
		
		if(!empty($film_id)){
			$time_start = time();
			$ch = curl_init();
			echo $url = C("WEB_URL").$link;
			//$url = "http://www.ygdy8.net/html/gndy/dyzz/20151201/49616.html";
			curl_setopt($ch, CURLOPT_URL, $url);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
			curl_setopt($ch, CURLOPT_TIMEOUT, 30);
			curl_setopt($ch, CURLOPT_HEADER, "Content-type:text/html;charset=utf-8");
			$res = curl_exec($ch);
			curl_close($ch);
			$res = iconv("gb2312", "UTF-8//IGNORE", $res);
			//echo $res;
			$pattern = '/<td style="WORD-WRAP: break-word" bgcolor="#fdfddf"><a href="(.*)">(.*)<\/a><\/td>/iUs';
			//$pattern1 = '/<img style="WIDTH: (.*); HEIGHT: (.*)" border="0" src="(.*)" width="(.*)" height="(.*)" alt="" \/>/iUs';
			$pattern1 = '/<span style="FONT-SIZE: 12px">(.*)<img (.*) src="(.*)"(.*)><br \/><br \/>/iUs';
			$pattern2 = '简　　介<br \/><br \/>(.*)<br \/><br \/>';
			preg_match_all($pattern, $res,$res1);
			preg_match($pattern1, $res,$res2);//封面链接
			preg_match($pattern2, $res,$res3);
			$this->InsertFilmLink($film_id,$res1[1],$res2[3]);
			//dump($res2);
			$time_over = time();
			echo "插入电影 ".$film_id." 用时：".($time_over-$time_start)."s\n";
		}else{
			echo "无执行数据！";
		}
	}
	
	public function get_film_info($film_id){
		if(isset($film_id)){
		$res = M('FilmDownloadLink')->field('film_link,pic_url')->where('film_id = '.$film_id)->select();
			
		}
		return $res;
	}
	
	
	
	
}	
	
	
	
	
?>